package com.kellycheung.chicken_go_go;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;

public class TextObject extends BaseObject {
	protected Bitmap[] bImgBmp;
	
	protected String sTxt;
	protected int iColor;
	protected int iAlpha;
	protected int iFontSize;
	
	public TextObject( GameScene p_gsScene , int p_iZOrder , GOAnchor p_goa , float p_fX , float p_fY , String p_sTxt ) {
		super(p_gsScene, p_iZOrder, p_goa, p_fX, p_fY);
		// TODO Auto-generated constructor stub
		
		try {
			this.sTxt = p_sTxt;
			this.iColor = Color.BLACK;
			this.iAlpha = 255;
			this.iFontSize = 80;
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "TextObject" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public void destroy() {
		try {
			if ( this.bImgBmp != null ) {
				for ( Bitmap b : this.bImgBmp ) {
					b.recycle();
				}
				
				this.bImgBmp = null;
			}
			
			super.destroy();
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "TextObject.destroy" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}

	@Override
	public void draw( Canvas p_c , Paint p_p ) {
		// TODO Auto-generated method stub
		try {
			Paint p = new Paint( p_p );
			float fTxtW;
			float fTxtH;
			GRectangle gr = new GRectangle( 0f , 0f , 0f , 0f ); 
			
			p.setColor( this.iColor );
			p.setAlpha( this.iAlpha );
			p.setTextSize( this.iFontSize );
			p.setTextAlign(Align.LEFT);
			
			String[] s = this.sTxt.trim().split("\n");
			for ( int i = 0 ; i < s.length ; i++ ) {
				Rect rTextBound = new Rect();
				p.getTextBounds( s[i].trim() , 0 , s[i].trim().length() , rTextBound );
				
				fTxtW = rTextBound.width();
				fTxtH = rTextBound.height();
				gr.set( this.gcCoord.fX + fTxtW , this.gcCoord.fY , this.gcCoord.fX , this.gcCoord.fY + fTxtH );
				gr.setY( this.gcCoord.fY + (i*fTxtH) );
				gr.set( gr.convCoord( GOAnchor.DownRight ).convCoord( this.goaAnchor ) );
				
				//gr.convCoord( this.goaAnchor );
				if( i == 0)
					p_c.drawText( s[i].trim() , gr.fLeft , gr.fDown , p );
				else
					p_c.drawText( s[i].trim() , gr.fLeft , gr.fDown+20f , p );
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "TextObject.draw" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}

	@Override
	public void preGameLoopLogic() {
	}

	@Override
	public void gameLoopLogic() {
	}

	@Override
	public void postGameLoopLogic() {
	}
}
